package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.common.enumeration.FeesBizTypeEnum;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.rule.chargeItem.ChargeItemDTO;
import io.github.talelin.latticy.dto.rule.chargeItem.ChargeItemPageDTO;
import io.github.talelin.latticy.dto.rule.chargeItem.UpdateChargeItemDTO;
import io.github.talelin.latticy.model.RuleKeyDO;
import io.github.talelin.latticy.service.ChargeItemService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.ChargeItemDO;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * @author generator@wulongbo
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/v1/charge-item")
@Validated
@PermissionModule(value = "计费项")
public class ChargeItemController {

    @Autowired
    private ChargeItemService chargeItemService;

    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "添加计费项")
    public CreatedVO create(@RequestBody @Validated ChargeItemDTO dto) {
        chargeItemService.addChargeItem(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "编辑计费项")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated UpdateChargeItemDTO dto) {
        chargeItemService.updateChargeItemById(dto, id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除计费项")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        chargeItemService.deleteChargeItemById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "查询计费项")
    public ChargeItemVO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return chargeItemService.getChargeItemVOById(id);
    }

    @LoginRequired
    @GetMapping("/list")
    @PermissionMeta(value = "查询计费项列表", mount = false)
    public FindResultVO list(Integer feesBizType) {
        return FindResultVO.success(chargeItemService.getChargeItemList(feesBizType));
    }

    @PostMapping("/page")
    @PermissionMeta(value = "查询计费项分页", mount = false)
    public PageResponseVO<ChargeItemDO> page(@RequestBody @Validated ChargeItemPageDTO dto) {
        IPage<ChargeItemDO> iPage = chargeItemService.getChargeItemPageByFeesBizType(dto.getPage()
                , dto.getCount(), dto.getName(), dto.getLevel(), dto.getBillType(), dto.getFeesBizType());
        return PageUtil.build(iPage);
    }

}
